"""
Case Type   : 兼容M*数据库time数据类型
Case Name   : time类型，输入格式为'[-][D] hh:mm:ss[.frac]'，[D]选项测试，
              部分测试点合理报错
Create At   : 2023.08.14
Owner       : z@wan005
Description :
    1、建表
    2、D有效值测试
    3、D无效值测试(D取值超范围、D取值含特殊字符
    4、添加D但返回时间超范围
    5、查询
    6、清理环境
Expect      :
    1、成功
    2、成功
    3、合理报错
    4、合理报错
    5、成功，time默认精度是0，故查询结果无小数位
    6、成功
History     :modified by z@wan005，2023.09.13，插入'' '12:59:12'格式，
             即D取值为空串，插入成功，同M侧保持一致，优化断言
             modified by z@wan005，2023.09.13，步骤4报错提示变更
             modified by zhanghuan96,2024.6.20，修改预期，适配新代码
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest04(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0004"
        self.tb_name = "tb_time_0004"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:建表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
                  f"create table {self.tb_name}(c1 time);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '--step2:D有效值测试;expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} values('-34 12:59:12.999999');" \
                  f"insert {self.tb_name} values('-0 838:59:59');" \
                  f"insert {self.tb_name} values('34 12:59:12.9999999');" \
                  f"insert {self.tb_name} values('0 838:59:59');" \
                  f"insert {self.tb_name} values('' '12:59:12');" \
                  f"insert {self.tb_name} values('-1:24:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(6, sql_res.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败' + text)

        text = '-step3:D无效值测试(D取值超范围、D取值含特殊字符);' \
               'expect:合理报错--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} values('35 12:59:12');" \
                  f"insert {self.tb_name} values('12.5 12:59:12');" \
                  f"insert {self.tb_name} values('&^2 24:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count('ERROR:'), '执行失败' + text)

        text = '--step4:添加D但返回时间超范围;expect:合理报错--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} values('1 838:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('ERROR:  date/time field value out of range',
                      sql_res, '执行失败' + text)

        text = '--step5:查询;expect:成功，time默认精度是0，故查询结果无小数位--'
        self.log.info(text)
        sql_cmd = f"select * from {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        res_list = ['-828:59:13', '-838:59:59', '828:59:13', '838:59:59',
                    '12:59:12', '-01:24:59']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '----step6:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
